Advanced Plots
November 10, 2014
Tony Fujs
long lat survivors direction group
24.0 54.9 340000 A 1
24.5 55.0 340000 A 1
25.5 54.5 340000 A 1
26.0 54.7 320000 A 1
27.0 54.8 300000 A 1
ggplot() +
geom_XXXX(
aes(x = long,
y = lat,
group = group),
data = troops)
ggplot() +
geom_path(
aes(x = long,
y = lat,
group = group),
data = troops)
# EDIT CODE
ggplot() +
geom_path(
aes(x = long,
y = lat,
group = group),
data = troops)
ggplot() +
geom_path(
aes(x = long,
y = lat,
group = group,
color = direction),
data = troops)
# EDIT CODE
ggplot() +
geom_path(
aes(x = long,
y = lat,
group = group,
color = direction),
data = troops)
Adjust line size to show number of survivors
ggplot() +
geom_path(
aes(x = long,
y = lat,
group = group,
color = direction,
size = survivors),
data = troops)
long lat city
24.0 55.0 Kowno
25.3 54.7 Wilna
26.4 54.4 Smorgoni
26.8 54.3 Moiodexno
27.7 55.2 Gloubokoe
# EDIT CODE
# (You must add a complete layer)
ggplot() +
geom_path(
aes(x = long,
y = lat,
group = group,
color = direction,
size = survivors),
data = troops)
Add cities on the plot (One point for each city)
# EDIT CODE
# (You must add a complete layer)
ggplot() +
geom_path(
aes(x = long,
y = lat,
group = group,
color = direction,
size = survivors),
data = troops) +
geom_point(
aes(
x = long,
y = lat),
data = cities)
# EDIT CODE
# (You must add a complete layer)
ggplot() +
geom_path(
aes(x = long,
y = lat,
group = group,
color = direction,
size = survivors),
data = troops) +
geom_point(
aes(
x = long,
y = lat),
data = cities)
Add city names on the plot beside each corresponding point
ggplot() +
geom_path(...) +
geom_point(...) +
geom_text(
aes(x = long,
y = lat,
label = city),
data = cities)
# Deal with overlapping
ggplot() +
geom_path(...) +
geom_point(...) +
geom_text(
aes(x = long,
y = lat,
label = city),
data = cities)
Deal with overlapping
ggplot() +
geom_path() +
geom_point() +
geom_text(
aes(x = long,
y = lat - 0.03,
label = city),
data = cities)
(Setting size inside or outside aes()?)
ggplot() +
geom_path(XXXX) +
geom_point(XXXX) +
geom_text(
aes(x = long,
y = lat - 0.03,
label = city),
size = 4,
data = cities)
# Which component of the gg does control the line colors?
ggplot() +
geom_path(XXXX) +
geom_point(XXXX) +
geom_text() +
?????
Customize the line colors
# Which component of the gg does control the line colors?
ggplot() +
geom_path(XXXX) +
geom_point(XXXX) +
geom_text() +
scale_color_manual(
values = c('grey50','red'))
long lat group order region
-87.46201 30.38968 1 1 alabama
-87.48493 30.37249 1 2 alabama
-87.52503 30.37249 1 3 alabama
-87.53076 30.33239 1 4 alabama
-87.57087 30.32665 1 5 alabama
-87.58806 30.32665 1 6 alabama
-87.59379 30.30947 1 7 alabama
-87.59379 30.28655 1 8 alabama
-87.67400 30.27509 1 9 alabama
-87.81152 30.25790 1 10 alabama
storenum lat long year new
1 36.33203 -94.14905 1965 0
1 36.33203 -94.14905 1970 0
1 36.33203 -94.14905 1975 0
1 36.33203 -94.14905 1980 0
1 36.33203 -94.14905 1985 0
1 36.33203 -94.14905 1990 0
1 36.33203 -94.14905 1995 0
1 36.33203 -94.14905 2000 0
1 36.33203 -94.14905 2005 0
2 36.26659 -93.13948 1965 0
Geographical information
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_point()
Initialize default ggplot & draw US map
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_XXXX(aes(group = group),
data = us,
colour="black", fill="#F7F3F3")
Initialize default ggplot & draw US map
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3")
Add points representing walmart stores
ggplot(aes(x = long, y = lat), data = walmart) +
geom_polygon(aes(group = group), data = us, colour="black", fill="#F7F3F3") +
????
Add points representing walmart stores
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point()
Change point color if NEW store
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point()
Change point color if NEW store
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = new))
Make sure geographic proportion are respected
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = new))
Make sure geographic proportion are respected
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = new)) +
coord_map()
Create a small multiple (ONE chart per year)
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = new)) +
coord_map()
Create a small multiple (ONE chart per year)
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = new)) +
coord_map() +
facet_wrap(~year)
Deal with overplotting (add transparency)
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = new)) +
coord_map() +
facet_wrap(~year)
Deal with overplotting (add transparency)
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = new,
alpha = new)) +
coord_map() +
facet_wrap(~year)
Deal with “new” treated as continuous
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = new,
alpha = new)) +
coord_map() +
facet_wrap(~year)
Deal with “new” treated as continuous
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = factor(new),
alpha = factor(new))) +
coord_map() +
facet_wrap(~year)
Customize transparency
myAlpha <- c(.4, 0.7)
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = factor(new),
alpha = factor(new))) +
coord_map() +
facet_wrap(~year)
Customize transparency
myAlpha <- c(.4, 0.7)
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = factor(new),
alpha = factor(new))) +
coord_map() +
scale_alpha_manual(values = myAlpha,
guide = 'none') +
facet_wrap(~year)
Customize colors
myPalette <- c('#1F78B4', '#FF0066')
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = factor(new),
alpha = factor(new))) +
coord_map() +
scale_alpha_manual(values = myAlpha,
guide = 'none') +
facet_wrap(~year)
Customize colors
myPalette <- c('#1F78B4', '#FF0066')
ggplot(aes(x = long, y = lat),
data = walmart) +
geom_polygon(aes(group = group),
data = us,
colour="black", fill="#F7F3F3") +
geom_point(aes(color = factor(new),
alpha = factor(new))) +
coord_map() +
scale_alpha_manual(values = myAlpha,
guide = 'none') +
scale_colour_manual(
values = myPalette,
guide = 'none') +
facet_wrap(~year)